package com.hw.paltfrom.service.system;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseAreaMapper;
import com.hw.paltfrom.mapper.HwBaseCountycoreMapper;
import com.hw.paltfrom.mapper.HwBaseOverrunstationMapper;
import com.hw.paltfrom.mapper.HwBaseUserMapper;
import com.hw.paltfrom.pojo.HwBaseArea;
import com.hw.paltfrom.pojo.HwBaseCountycore;
import com.hw.paltfrom.pojo.HwBaseOverrunstation;
import com.hw.paltfrom.pojo.HwBaseUser;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.*;
import com.hw.paltfrom.vo.BaseUserVo;
import com.hw.paltfrom.vo.StandCommandTaskVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.*;

/**
 * 系统用户
 * 崔孟超
 * 2019年11月6日15:21:42
 */
@Service
public class BaseUserServer {
    @Autowired
    private HwBaseUserMapper hwBaseUserMapper;
    @Autowired //县治超中心 HW_Base_CountyCore
    private HwBaseCountycoreMapper hwBaseCountycoreMapper;
    @Autowired  // 超限检测站基础信息 HW_Base_OverrunStation
    private HwBaseOverrunstationMapper hwBaseOverrunstationMapper;
    @Autowired
    private HwBaseAreaMapper hwBaseAreaMapper;
    /**
     * 用户的添加
     * @param vo
     * @return
     */
    public Map<String, Object> insert(BaseUserVo vo) {
        //工号账户（唯一）
        String fEncode = vo.getfEncode();
        //账号唯一
        String fAccount = vo.getfAccount();
        //微信
        String fWechat = vo.getfWechat();
        //手机号
        String fMobile = vo.getfMobile();
        String shengri = vo.getfBirthday();

        Wrapper<HwBaseUser> namewrapper = new EntityWrapper<HwBaseUser>();
        if (fEncode.length() == 0) {
            return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
        } else if (fEncode.length() > 0) {
            namewrapper.eq("F_EnCode", fEncode);
            List<HwBaseUser> baseUsersList = hwBaseUserMapper.selectList(namewrapper);
            if (!baseUsersList.isEmpty()) {
                return ResponeResultUtil.fail(HWSystemCode.ENCODE_EXIT);
            }
        }
        if (fAccount.length() == 0) {
            return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
        } else if (fAccount.length() > 0) {
            Wrapper<HwBaseUser> wrapper = new EntityWrapper<HwBaseUser>();
            wrapper.eq("F_Account", fAccount);
            List<HwBaseUser> baseUsersList = hwBaseUserMapper.selectList(wrapper);
            if (!baseUsersList.isEmpty()) {
                return ResponeResultUtil.fail(HWSystemCode.ACCOUNT_EXIT);
            }
        }
        //生成ID、创建时间、插入数据库
        HwBaseUser hwBaseUser = new HwBaseUser();
        hwBaseUser.setfUserid(RandomNumberUtil.getNumber());
        hwBaseUser.setfEncode(fEncode);
        hwBaseUser.setfAccount(fAccount);
        hwBaseUser.setfPassword(PassWordUtils.entryptPassword(vo.getfPassword()));
        //密码秘钥（本项目不用）
        // hwBaseUser.setfSecretkey(vo.getfSecretkey());
        hwBaseUser.setfRealname(vo.getfRealname());
        hwBaseUser.setfNickname(vo.getfNickname());
        hwBaseUser.setfHeadicon(vo.getfHeadicon());
        hwBaseUser.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfRealname()));
        hwBaseUser.setfSimplespelling(LanguageUtil.getAllFirstLetter(vo.getfRealname()));
        hwBaseUser.setfGender(vo.getfGender());
        if (!"".equals(shengri)){
            long beginlong = new Long(shengri);
            hwBaseUser.setfBirthday(new Date(beginlong));
        }
        hwBaseUser.setfMobile(fMobile);
        hwBaseUser.setfTelephone(vo.getfTelephone());
        hwBaseUser.setfEmail(vo.getfEmail());
        hwBaseUser.setfOicq(vo.getfOicq());
        hwBaseUser.setfWechat(fWechat);
        hwBaseUser.setfSortcode(vo.getfSortcode());
        hwBaseUser.setfDeletemark(vo.getfDeletemark());
        hwBaseUser.setfEnabledmark(vo.getfEnabledmark());
        hwBaseUser.setfDescription(vo.getfDescription());
        hwBaseUser.setfCreatedate(new Timestamp(System.currentTimeMillis()));
        hwBaseUser.setfCreateuserid(vo.getfCreateuserid());
        hwBaseUser.setfCreateusername(vo.getfCreateusername());
        hwBaseUser.setfAreaid(vo.getfAreaid());
        hwBaseUser.setfType(vo.getfType());
        hwBaseUser.setfTypecode(vo.getfTypecode());
        hwBaseUser.setfLockcode(vo.getfLockcode());
        Integer insert = hwBaseUserMapper.insert(hwBaseUser);
        if (insert != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }

    /**
     * 用户的编辑修改
     * @param vo
     * @return
     */
    public Map<String, Object> update(BaseUserVo vo) {
        String fUserid = vo.getfUserid();
        //工号账户（唯一）
        String fEncode = vo.getfEncode();
        //账号唯一
        String fAccount = vo.getfAccount();
        String shengri = vo.getfBirthday();
        Wrapper<HwBaseUser> wrapper = new EntityWrapper<HwBaseUser>();
        wrapper.eq("F_EnCode", fEncode).and().eq("F_Account", fAccount).eq("F_UserId", fUserid);
        HwBaseUser hwBaseUser = new HwBaseUser();
        if (vo.getfDeletemark() == 1) {
            hwBaseUser.setfDeletemark(vo.getfDeletemark());
            hwBaseUser.setfModifydate(new Timestamp(System.currentTimeMillis()));
            hwBaseUser.setfModifyuserid(vo.getfModifyuserid());
            hwBaseUser.setfModifyusername(vo.getfModifyusername());
            Integer update = hwBaseUserMapper.update(hwBaseUser, wrapper);
            if (update != 1) {
                return ResponeResultUtil.fail(HWSystemCode.FAIL);
            }
        } else {
            //微信
            String fWechat = vo.getfWechat();
            //手机号
            String fMobile = vo.getfMobile();
            hwBaseUser.setfEncode(fEncode);
            hwBaseUser.setfAccount(fAccount);
            if (!"未修改".equals(vo.getfPassword())){
                hwBaseUser.setfPassword(PassWordUtils.entryptPassword(vo.getfPassword()));
            }
            hwBaseUser.setfSecretkey(vo.getfSecretkey());
            hwBaseUser.setfRealname(vo.getfRealname());
            hwBaseUser.setfNickname(vo.getfNickname());
            hwBaseUser.setfHeadicon(vo.getfHeadicon());
            hwBaseUser.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfRealname()));
            hwBaseUser.setfSimplespelling(LanguageUtil.getAllFirstLetter(vo.getfRealname()));
            hwBaseUser.setfGender(vo.getfGender());
            if (!"".equals(shengri)){
                long beginlong = new Long(shengri);
                hwBaseUser.setfBirthday(new Date(beginlong));
            }
            hwBaseUser.setfMobile(fMobile);
            hwBaseUser.setfTelephone(vo.getfTelephone());
            hwBaseUser.setfEmail(vo.getfEmail());
            hwBaseUser.setfOicq(vo.getfOicq());
            hwBaseUser.setfWechat(fWechat);
            hwBaseUser.setfSortcode(vo.getfSortcode());
            hwBaseUser.setfEnabledmark(vo.getfEnabledmark());
            hwBaseUser.setfDescription(vo.getfDescription());
            hwBaseUser.setfDescription(vo.getfDescription());
            hwBaseUser.setfModifydate(new Timestamp(System.currentTimeMillis()));
            hwBaseUser.setfModifyusername(vo.getfModifyusername());
            hwBaseUser.setfModifyuserid(vo.getfModifyuserid());
            hwBaseUser.setfAreaid(vo.getfAreaid());
            hwBaseUser.setfType(vo.getfType());
            hwBaseUser.setfTypecode(vo.getfTypecode());
            hwBaseUser.setfLockcode(vo.getfLockcode());
            Wrapper<HwBaseUser> wrapperUpdate = new EntityWrapper<HwBaseUser>();
            wrapperUpdate.eq("F_Account", fAccount).eq("F_UserId", fUserid);
            Integer update = hwBaseUserMapper.update(hwBaseUser, wrapperUpdate);
            if (update != 1) {
                return ResponeResultUtil.fail(HWSystemCode.FAIL);
            }
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }

    /**
     * 用户初使化
     * @return
     */
    public Map<String, Object> initIaLiZatIonUser(BaseUserVo vo) {
        //工号账户（唯一）
        String fEncode = vo.getfEncode();
        //账号唯一
        String fAccount = vo.getfAccount();
        String fUserid = vo.getfUserid();
        String fPassword = vo.getfPassword();
        String password = PassWordUtils.entryptPassword(fPassword);
        Wrapper<HwBaseUser> wrapper = new EntityWrapper<HwBaseUser>();
        wrapper.eq("F_EnCode", fEncode).and().eq("F_Account", fAccount).eq("F_UserId", fUserid);
        HwBaseUser hwBaseUser = new HwBaseUser();
        hwBaseUser.setfEncode(fEncode);
        hwBaseUser.setfAccount(fAccount);
        hwBaseUser.setfPassword(password);
        Integer update = hwBaseUserMapper.update(hwBaseUser, wrapper);
        if (update != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }


    /**
     * 用户停用
     *
     * @return
     */
    public Map<String, Object> diSaBleUser(BaseUserVo vo) {
        //工号账户（唯一）
        String fEncode = vo.getfEncode();
        //账号唯一
        String fAccount = vo.getfAccount();
        String fUserid = vo.getfUserid();
        Integer integer = vo.getfEnabledmark();
        Wrapper<HwBaseUser> wrapper = new EntityWrapper<HwBaseUser>();
        wrapper.eq("F_EnCode", fEncode).and().eq("F_Account", fAccount).eq("F_UserId", fUserid);
        HwBaseUser hwBaseUser = new HwBaseUser();
        //锁定标记，默认0（未锁定），1锁定
        if (integer ==1) {
            hwBaseUser.setfEnabledmark(1);
            hwBaseUser.setfLockstartdate(new Timestamp(System.currentTimeMillis()));
            Integer update = hwBaseUserMapper.update(hwBaseUser, wrapper);
            if (update != 1) {
                return ResponeResultUtil.fail(HWSystemCode.FAIL);
            }
            return ResponeResultUtil.success(HWSystemCode.SUCCESS);
        } else if (integer == 0) {
            hwBaseUser.setfEnabledmark(0);
            hwBaseUser.setfLockenddate(new Timestamp(System.currentTimeMillis()));
            Integer update = hwBaseUserMapper.update(hwBaseUser, wrapper);
            if (update != 1) {
                return ResponeResultUtil.fail(HWSystemCode.FAIL);
            }
            return ResponeResultUtil.success(HWSystemCode.SUCCESS);
        }
        return null;
    }

    /**
     * 用户遍历+分页+模糊查询
     * @param vo
     * @return
     */
    public Map<String, Object> queryAllList(BaseUserVo vo) {
        String fAeraid = vo.getfAeraid();
        String fAccount = vo.getfAccount();
        Integer pageNum = vo.getPageNum();
        Integer pageSize = vo.getPageSize();
        Wrapper<HwBaseUser> namewrapper = new EntityWrapper<HwBaseUser>();
//        if (StringUtils.isNotBlank(fAccount)) {
//            namewrapper.like("F_Account", fAccount).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
//        } else if (StringUtils.isNotBlank(fAeraid)) {
//            namewrapper.like("F_AreaId", fAeraid).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
//        } else if (!StringUtils.isNotBlank(fAccount) && !StringUtils.isNotBlank(fAeraid)) {
//            namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
//        }


//        // 查全市 不传fAreacode参数
//        if (StringUtils.isNotBlank(fAreacode)) {
//            namewrapper.eq("F_AreaCode", fAreacode);
//        }
        PageUtils<BaseUserVo> pageUtils = new PageUtils<BaseUserVo>(pageNum, pageSize);
        if (StringUtils.isNotBlank(fAccount)) {
            namewrapper.like("F_Account", fAccount);
        }
        if (StringUtils.isNotBlank(fAeraid)) {
            namewrapper.eq("F_AreaId", fAeraid);
        }
        namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
        List<HwBaseUser> hwBaseUserList = hwBaseUserMapper.selectList(namewrapper);
        List<BaseUserVo> baseUserVos=new ArrayList<>();
        for (HwBaseUser hwBaseUser : hwBaseUserList) {
            BaseUserVo basevo=new BaseUserVo();
            basevo.setfUserid(hwBaseUser.getfUserid());
            basevo.setfAccount(hwBaseUser.getfAccount());
            basevo.setfEncode(hwBaseUser.getfEncode());
            basevo.setfPassword(hwBaseUser.getfPassword());
            basevo.setfSecretkey(hwBaseUser.getfSecretkey());
            basevo.setfRealname(hwBaseUser.getfRealname());
            basevo.setfNickname(hwBaseUser.getfNickname());
            basevo.setfHeadicon(hwBaseUser.getfHeadicon());
            basevo.setfQuickquery(hwBaseUser.getfQuickquery());
            basevo.setfSimplespelling(hwBaseUser.getfSimplespelling());
            basevo.setfGender(hwBaseUser.getfGender());
            Date date = hwBaseUser.getfBirthday();
            if (date!=null){
                basevo.setfBirthday(date.toString());
            }
            basevo.setfMobile(hwBaseUser.getfMobile());
            basevo.setfTelephone(hwBaseUser.getfTelephone());
            basevo.setfEmail(hwBaseUser.getfEmail());
            basevo.setfOicq(hwBaseUser.getfOicq());
            basevo.setfWechat(hwBaseUser.getfWechat());
            basevo.setfLockstartdate(hwBaseUser.getfLockstartdate());
            basevo.setfLockenddate(hwBaseUser.getfLockenddate());
            basevo.setfSortcode(hwBaseUser.getfSortcode());
            basevo.setfDeletemark(hwBaseUser.getfDeletemark());
            basevo.setfEnabledmark(hwBaseUser.getfEnabledmark());
            basevo.setfDescription(hwBaseUser.getfDescription());
            basevo.setfCreatedate(hwBaseUser.getfCreatedate());
            basevo.setfCreateuserid(hwBaseUser.getfCreateuserid());
            basevo.setfCreateusername(hwBaseUser.getfCreateusername());
            basevo.setfModifydate(hwBaseUser.getfModifydate());
            basevo.setfModifyuserid(hwBaseUser.getfModifyuserid());
            basevo.setfModifyusername(hwBaseUser.getfModifyusername());
            basevo.setfAeraid(hwBaseUser.getfAreaid());
            basevo.setfType(hwBaseUser.getfType());
            basevo.setfTypecode(hwBaseUser.getfTypecode());
            basevo.setfLockcode(hwBaseUser.getfLockcode());
            Wrapper<HwBaseCountycore> wrapper = new EntityWrapper<HwBaseCountycore>();
            wrapper.eq("F_CountyCoreCode",hwBaseUser.getfTypecode());
            List<HwBaseCountycore> hwBaseCountycores = hwBaseCountycoreMapper.selectList(wrapper);
            if (hwBaseCountycores.size()!=0){
                basevo.setfEnterpriseName(hwBaseCountycores.get(0).getfCountycorename());
            }
            Wrapper<HwBaseOverrunstation> Overrunstation = new EntityWrapper<HwBaseOverrunstation>();
            Overrunstation.eq("F_OverrunCode",hwBaseUser.getfTypecode());
            List<HwBaseOverrunstation> hwBaseOverrunstations = hwBaseOverrunstationMapper.selectList(Overrunstation);
            if (hwBaseOverrunstations.size()!=0){
                basevo.setfEnterpriseName(hwBaseOverrunstations.get(0).getfOverrunname());
            }
            if (hwBaseOverrunstations.size()==0&&hwBaseCountycores.size()==0){
                basevo.setfEnterpriseName("");
            }
            baseUserVos.add(basevo);
        }
        Object info = pageUtils.getPageResult(baseUserVos);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
    }

    /**
     *
     * @param
     * @return
     */
    public Map<String, Object> particularsUser() {

        Wrapper<HwBaseArea> namewrapper = new EntityWrapper<HwBaseArea>();
        namewrapper.eq("F_DeleteMark" ,"0").orderBy("F_SortCode");
        List<HwBaseArea> hwBaseAreasList = hwBaseAreaMapper.selectList(namewrapper);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, hwBaseAreasList);
    }

    public Map<String, Object> popUpBox(BaseUserVo vo) {
        //01代表市县 02代表站
        String popover = vo.getPopover();
        List<Map<String,Object>>list=new ArrayList<>();
        if ("01".equals(popover)){
            Wrapper<HwBaseCountycore> wrapper = new EntityWrapper<HwBaseCountycore>();
            wrapper.eq("F_DeleteMark",0).orderBy("F_SortCode");
            List<HwBaseCountycore> hwBaseCountycores = hwBaseCountycoreMapper.selectList(wrapper);
            for (HwBaseCountycore hwBaseCountycore : hwBaseCountycores) {
                Map<String,Object>map=new HashMap<>(); //F_CountyCoreCode
                map.put("F_OverrunCode",hwBaseCountycore.getfCountycorecode());
                map.put("F_CountyCoreName",hwBaseCountycore.getfCountycorename());
                list.add(map);
            }
        }
        if ("03".equals(popover)){
            return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, list);
          }
        if ("02".equals(popover)){
            Wrapper<HwBaseOverrunstation> wrapper = new EntityWrapper<HwBaseOverrunstation>();
            wrapper.eq("F_DeleteMark",0).orderBy("F_SortCode");
            List<HwBaseOverrunstation> hwBaseOverrunstations = hwBaseOverrunstationMapper.selectList(wrapper);
            for (HwBaseOverrunstation hwBaseOverrunstation : hwBaseOverrunstations) {
                Map<String,Object>map=new HashMap<>(); //F_OverrunCode F_OverrunName
                map.put("F_OverrunCode",hwBaseOverrunstation.getfOverruncode());
                map.put("F_CountyCoreName",hwBaseOverrunstation.getfOverrunname());
                list.add(map);
            }
        }
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, list);
    }
}
